<?php


declare(strict_types=1);

namespace app\operations\controller\admin;

use app\operations\model\Brand as BrandModel;
use app\operations\model\Store as StoreModel;
use sheep\controller\Backend;
use think\facade\Db;
use think\Request;
use think\Response;

//品牌申请
class Brand extends Backend
{

    protected $model = null;
    protected $storeModel = null;


    protected function initialize()
    {
        $this->model = new BrandModel;
        $this->storeModel = new StoreModel;
    }


    /**
     * 查看
     *
     * @return Response
     */
    public function list(Request $request): Response
    {
        $params = $this->request->only([
            'id'
        ]);
        $data = $this->storeModel->findOrFail($params['id']);
        $list = $this->model->where('organ_id',$data->organ_id)->where('type',$data->type)->select()->toArray();


//        dd($list);

        foreach($list as &$val){

            $val['category_text'] = $this->model->getCategoryNamesAttr(null, $val);
        }

        return success('获取成功', $list);
    }



    /**
     * 申请添加
     *
     * @return \think\Response
     */
    public function add(Request $request)
    {
        $params = $this->request->only(['id','logo', 'trademark_registration_number', 'qualification_certificate', 'category_ids','expiration_date','auth_type']);
        $data = $this->storeModel->findOrFail($params['id']);
        $params['organ_id'] = $data->organ_id;
        $params['type'] = $data->type;
        unset($params['id']);

        $this->svalidate($params, ".add");

        $data = Db::transaction(function () use ($params) {

            $this->model->save($params);

        });

        return success('保存成功', $data);
    }


    /**
     * 重新申请
     *
     * @param  $id
     * @return \think\Response
     */
    public function edit(Request $request, $id)
    {

        $params = $this->request->only(['logo', 'trademark_registration_number', 'qualification_certificate', 'category_ids','expiration_date','auth_type']);

        $params['audit_status'] = '0';
        $params['remarks'] = '';

        $this->svalidate($params);

        $id = intval($id);

        Db::transaction(function () use ($id, $params) {

            $data = $this->model->where('id',$id)->where('type',$params['type'])->find();
            $data->save($params);

        });

        return success('更新成功');
    }




}
